package com.gcb.service.impl;

import com.gcb.dao.OrderSettingDao;
import com.gcb.pojo.OrderSetting;
import com.gcb.service.OrderSettingService;
import org.apache.dubbo.config.annotation.DubboService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.*;

/**
 * @Author 35235
 * @Date 2021/8/8 13:46
 * @Version 1.0
 */
@DubboService(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public boolean updateNumber(OrderSetting orderSetting) {
        int i = orderSettingDao.updateNumber(orderSetting);
        if (0 == i) {
            return false;
        } else {
            return true;
        }
    }

    public boolean add(OrderSetting orderSetting) {
        int i = orderSettingDao.add(orderSetting);
        if (0 == i) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(Date date) {
        Map<String, Object> map = new HashMap<>();
        map.put("year", date.getYear() + 1900);
        map.put("month", date.getMonth() + 1);
        System.out.println(map);
        List<OrderSetting> orderSettings = orderSettingDao.getByMonth(map);
        List<Map> list = new ArrayList<>();
        if (orderSettings != null) {
            for (OrderSetting orderSetting : orderSettings) {
                int date2 = orderSetting.getOrderDate().getDate();
                Map data = new HashMap();
                data.put("date", date2);
                data.put("number", orderSetting.getNumber());
                data.put("reservations", orderSetting.getReservations());
                list.add(data);
            }

        }
        return list;
    }

    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        OrderSettingDao orderSettingDao = (OrderSettingDao) context.getBean("orderSettingDao");
        Map map = new HashMap();
        map.put("month", 8);
        map.put("year", 2021);
        List<OrderSetting> orderSettings = orderSettingDao.getByMonth(map);
        List<Map> list = new ArrayList<>();
        if (orderSettings != null) {
            for (OrderSetting orderSetting : orderSettings) {
                int date = orderSetting.getOrderDate().getDate();
                Map data = new HashMap();
                data.put("date", date);
                data.put("number", orderSetting.getNumber());
                data.put("reservations", orderSetting.getReservations());
                list.add(data);
            }
        }
        System.out.println(orderSettings);
    }
}
